package dao.impl;

import dao.StudentDao;
import entity.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {

    public static final String PATH = "/home/Hanserwei/IdeaProjects/java-learn2/Day16-03/students.txt";

    @Override
    public Boolean register(Student student) {
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(PATH, true))) {
            bufferedWriter.write(student.toString());
            bufferedWriter.newLine();
            return true;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student selectByName(String name) {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(PATH))) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] properties = line.split("-");
                if (properties[1].equals(name)) {
                    return new Student(Integer.parseInt(properties[0]), properties[1], properties[2], properties[3]);
                }
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student selectByEmail(String email) {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(PATH))) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] properties = line.split("-");
                if (properties[3].equals(email)) {
                    return new Student(Integer.parseInt(properties[0]), properties[1], properties[2], properties[3]);
                }
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Integer selectLastId() {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(PATH))) {
            String line;
            // 查询到最后一行
            String lastLine = null;
            while ((line = bufferedReader.readLine()) != null) {
                lastLine = line;
            }
            if (lastLine != null) {
                String[] properties = lastLine.split("-");
                return Integer.parseInt(properties[0]);
            }
            return 0;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student selectWithNameAndPassword(String name, String password) {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(PATH))) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] properties = line.split("-");
                if (properties[1].equals(name) && properties[2].equals(password)) {
                    return new Student(Integer.parseInt(properties[0]), properties[1], properties[2], properties[3]);
                }
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Student> selectAllStudents() {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(PATH))) {
            String line;
            List<Student> studentList = new ArrayList<>();
            while ((line = bufferedReader.readLine()) != null) {
                String[] properties = line.split("-");
                Student student = new Student(Integer.parseInt(properties[0]), properties[1], properties[2], properties[3]);
                studentList.add(student);
            }
            return studentList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Student> selectStudentLikeName(String name) {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(PATH))) {
            String line;
            List<Student> studentList = new ArrayList<>();
            while ((line = bufferedReader.readLine()) != null) {
                String[] properties = line.split("-");
                if (properties[1].contains(name)) {
                    Student student = new Student(Integer.parseInt(properties[0]), properties[1], properties[2], properties[3]);
                    studentList.add(student);
                }
            }
            return studentList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
